<template>
    <view>
		<cu-editor
        style="background-color: #ffffff;"
        name="img"
        ref="editor"
        :url="uploadUrl"
        :header="header"
        :formData="formData"
        :content="content"
        @before="onUploadBefore"
        @update="onUpdate"
        @save="onSave">
    </cu-editor>
    </view>
</template>
<script>

const app = getApp();

import cuEditor from '../../../components/cu-editor/cu-editor'
export default {
    components: {
      cuEditor
    },
    data() {
        return {
            title:"公告",
            association_id: 0,
            header: {
                'content-type': 'application/json',
            },
            formData: {
                token:'',
                SafeCode:uni.getStorageSync('SafeCode'),
            },
            content: '',
            uploadUrl: app.globalData.ApiUrl + '/api/Android/uploadNoticeImg',
        }
    },


    onLoad(option) {

        this.association_id = option.id;

        //生成32个随机字符串
        let chars = ['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
        let res = "";
        for(var i = 0; i < 32 ; i ++) {
            var id = Math.ceil(Math.random()*35);
            res += chars[id];
        }
        this.formData.token = res;
    },


    methods: {
        onUploadBefore(file) {
            console.log(file)
        },
        onUpdate(html) {
            console.log(html);
        },
        onSave(data) {
            this.$myFunction.getData(app.globalData.ApiUrl + '/api/Android/uploadNotice',{
                association_id: this.association_id,
                title:data.title,
                content: data.html,
                token: this.formData.token,
                SafeCode: uni.getStorageSync('SafeCode')
            }).then((res)=>{
                console.log(res)
                if(res.code == 1){
                    uni.showToast({
                        title: '发布成功',
                        icon: 'success',
                        duration: 2000
                    })
                    setTimeout(()=>{
                        uni.reLaunch({
                            url: '/pages/association/detail/detail?id='+this.association_id
                        })
                    },2000)
                }else{
                    uni.showToast({
                        title: res.data.msg,
                        icon: 'none',
                        duration: 2000
                    })
                }
            })
        },
    },
}
</script>